﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Interactivity;

namespace Songhay.InteractionTriggers
{
    using Extensions;

    /// <summary>
    /// Interaction Trigger for Collapsing a DataGridRow.
    /// </summary>
    /// <remarks>
    /// For more, see “how to hide a datagrid detail row”
    /// [http://stackoverflow.com/questions/5158926/how-to-hide-a-datagrid-detail-row]
    /// </remarks>
    public class DataGridToggleRowAction : TriggerAction<ToggleButton>
    {
        /// <summary>
        /// Invokes the action.
        /// </summary>
        /// <param name="parameter">
        ///     The parameter to the action.
        ///     If the action does not require a parameter,
        ///     the parameter may be set to a null reference.
        /// </param>
        protected override void Invoke(object parameter)
        {
            var row = this.AssociatedObject.FindAncestor<DataGridRow>();

            if(row == null) return;

            if(this.AssociatedObject.IsChecked == true)
                row.DetailsVisibility = Visibility.Visible;
            else
                row.DetailsVisibility = Visibility.Collapsed;
        }
    }
}
